<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="textName"
      ></el-input>
      <el-button @click="clearInput">清除</el-button>
      <el-button type="primary" @click="sherchHobby">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="getList">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click.native="ediHobby(row.id)"
            >编辑</el-button
          >
          <el-popconfirm>
            <template #reference>
              <el-button size="mini" type="danger" @click="delHobby(row.id)"
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button
        size="small"
        icon="el-icon-plus"
        type="primary"
        @click="addHobby"
      >
        新增</el-button
      >
      <!-- 新增弹框 -->

      <el-dialog
        title="提示"
        :visible="dialogVisible"
        width="30%"
        @open="openDepart"
        @close="closeDepart"
      >
        <template #default>
          <el-form
            ref="addForm"
            :model="Numberform"
            :rules="rules"
            label-width="100px"
          >
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="Numberform.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="des">
              <el-input v-model="Numberform.des" />
            </el-form-item>
          </el-form>
        </template>
        <template #footer>
          <span class="dialog-footer">
            <!--
          取消
          1. 弹框关闭
          2. 清空当前的表单数据 和 校验痕迹
         -->
            <el-button @click="closeDepart">取 消</el-button>
            <el-button type="primary" @click="confirm">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from "./API";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    // const checkName = async (rule, value, callback) => {
    //   const res = await request.fatgetNumber({
    //     _page: 1, // 当前第几页
    //     _limit: 100, // 每页条数
    //   });
    //   let arr = res.data;
    //   let nameList = [];
    //   if (this.Numberform.id) {
    //     arr.forEach((item) => {
    //       if (item.name !== this.isName) {
    //         nameList.push(item.name);
    //       }
    //     });
    //   } else {
    //     arr.forEach((item) => {
    //       nameList.push(item.name);
    //     });
    //   }
    //   if (nameList.includes(value)) {
    //     callback(new Error("任务名称已存在"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      getList: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0,
      Numberform: {
        name: "",
        des: "",
        id: "",
      },
      rules: {
        name: [{ required: true, message: "角色名称必填", trigger: "blur" }],
        des: [{ required: true, message: "描述必填", trigger: "blur" }],
      },
      dialogVisible: false,
      textName: "",
    };
  },
  created() {
    this.getNumber();
    this.addNumber();
  },
  methods: {
    // 渲染数据
    async getNumber() {
      const res = await request.fatgetNumber(this.params);
      console.log(res);
      this.getList = res.data;
      this.total = Number(res.headers["x-total-count"]);
      // this.textName = res.data.name;
    },
    pageChange(page) {
      this.params._page = page;
      // console.log(123);
      this.getNumber();
    },
    // 调用新增数据
    async addNumber() {
      this.Numberform.id = uuidv4();
      console.log(uuidv4(), 45);
      const res = await request.fataddNumber(this.Numberform);
      console.log(res);
    },

    addHobby() {
      this.dialogVisible = true;
    },
    openDepart() {
      this.dialogVisible = true;
    },
    closeDepart() {
      this.dialogVisible = false;
      this.resetForm();
    },

    resetForm() {
      this.$refs.addForm.resetFields();
      this.Numberform = {
        name: "",
        des: "",
      };
    },
    confirm() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.Numberform.id) {
            await request.fatediNumber(this.Numberform);
            this.$message.success("编辑成功");
            this.dialogVisible = false;
          } else {
            await request.fataddNumber(this.Numberform);
            this.resetForm();
            this.$message.success("新增成功");
            this.dialogVisible = false;
          }
        }
      });
    },
    // 数据回填
    async ediHobby(id) {
      console.log(id);

      this.dialogVisible = true;
      const res = await request.fathuitianNumber(id);
      this.Numberform = res.data;
    },
    // 删除数据
    delHobby(id) {
      console.log(id);
      this.$confirm("确认删除此数据吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 删除逻辑
          await request.fatdelNamber(id);
          this.$message.success("删除成功");
          this.getNumber();
        })
        .catch(() => {});
    },
    // 清空输入框
    clearInput() {
      this.textName = "";
      this.getNumber();
    },
    // 搜索数据
    async sherchHobby() {
      const res = await request.fatsearchNamber(this.textName);

      this.getList = res.data;
      console.log(res);
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
